package com.ra.config.security;

import com.alibaba.fastjson.JSON;
import com.ra.constant.AuthConstant;
import com.ra.pojo.RespBean;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.ra.enums.StatusCodeEnum.AUTHORIZED;

/**
 * Author : 今晚几点睡_Ada
 * Date: 2023/3/9
 * Time: 13:54
 * 注:
 */
@Component
public class RestfulAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        httpServletResponse.setContentType(AuthConstant.APPLICATION_JSON);
        httpServletResponse.getWriter().write(JSON.toJSONString(RespBean.error(AUTHORIZED)));
    }
}
